<template>
  <div class="post-detail-page">
    <modal-view title="删除文章" :visible="modalIsVisible" @modal-on-confirm="hideAndDelete" @modal-on-close="modalIsVisible = false">
      <p>确认要删除这篇文章吗？</p>
    </modal-view>
    <article class="w-75 mx-auto mb-5 pb-3" v-if="currentPost">
      <img :src="currentImageUrl" alt="currentPost.title" class="rounded-lg img-fluid my-4"
           v-if="currentImageUrl">
      <h2 class="mb-4">{{ currentPost.title }}</h2>
      <div class="user-profile-component border-top border-bottom py-3 mb-5 align-items-center row g-0">
        <div class="col">
          <user-profile :user="currentPost.author" v-if="typeof currentPost.author === 'object'"></user-profile>
        </div>
        <span class="text-muted col text-right font-italic">发表于：{{ currentPost.createdAt }}</span>
      </div>
      <div v-html="currentHTML"></div>
      <div class="btn-group mt-5" v-if="showEditArea">
        <router-link :to="{ name:'create',query:{id:currentPost._id}}" type="button" class="btn btn-success">编辑
        </router-link>
        <button type="button" class="btn btn-danger" @click="modalIsVisible = true">删除</button>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import {computed, defineComponent, onMounted,ref} from "vue";
import MarkdownIt from "markdown-it";
import {PostProps, GolbalDataProps, ImageProps, UserProps,ResponseType} from "@/store";
import {useRoute, useRouter} from "vue-router";
import {useStore} from "vuex";
import UserProfile from "@/components/UserProfile.vue";
import ModalView from "@/components/ModalView.vue";
import createMessage from "@/hooks/createMessage";

export default defineComponent({
  components: {
    UserProfile,
    ModalView
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const store = useStore<GolbalDataProps>()
    const currenId = route.params.id
    const md = new MarkdownIt()
    const modalIsVisible = ref(false)


    onMounted(() => {
      // 文章列表
      store.dispatch('fetchPost', currenId)
    })
    const currentPost = computed<PostProps>(() => store.getters.getPost(currenId))

    const showEditArea = computed(() => {
      const {isLogin,_id} = store.state.user
      if(currentPost.value && currentPost.value.author && isLogin) {
        const postAuthor = currentPost.value.author as UserProps
        return postAuthor._id === _id
      }else {
        return false
      }
    });
    // console.log(currentPost)
    // eslint-disable-next-line vue/return-in-computed-property
    const currentHTML = computed(() => {
      const {content} = currentPost.value
      if (currentPost.value && content) {
        return md.render(content)
      }
    })
    const currentImageUrl = computed(() => {
      if (currentPost.value && currentPost.value.image) {
        const {image} = currentPost.value
        return (image as ImageProps).url
      } else {
        return null
      }
    })
    const hideAndDelete = () => {
      modalIsVisible.value = false
      store.dispatch('deletePost',currenId).then((rwaData:ResponseType<PostProps>) => {
        createMessage('删除成功，2秒后跳转至专栏页','success')
        setTimeout(() => {
          router.push({name:'column',params:{id:rwaData.data.column}})
        },2000)
      })
    };
    return {
      currentPost,
      currentImageUrl,
      currentHTML,
      showEditArea,
      modalIsVisible,
      hideAndDelete
    };
  }
})
</script>

<style>

</style>
